package com.eamois.dao.manual.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.eamois.dao.impl.BaseDaoImpl;
import com.eamois.dao.manual.EnsipDao;
import com.eamois.domain.manual.Engine;
import com.eamois.domain.manual.Ensip;
import com.eamois.domain.manual.EnsipType;

public class EnsipDaoImpl extends BaseDaoImpl<com.eamois.domain.manual.Ensip>
		implements EnsipDao {

	@SuppressWarnings("unchecked")
	public Collection<com.eamois.domain.manual.Ensip> findAllEnsip() {
		return (Collection<com.eamois.domain.manual.Ensip>) this
				.getHibernateTemplate().execute(new HibernateCallback() {

					@Override
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						// TODO Auto-generated method stub
						Criteria criteria = session
								.createCriteria(com.eamois.domain.manual.Ensip.class);
						return criteria.list();
					}
				});
	}

	@SuppressWarnings("unchecked")
	public Collection<com.eamois.domain.manual.Ensip> findEnsipByParent(
			final com.eamois.domain.manual.Ensip ensip) {
		// TODO Auto-generated method stub
		return (Collection<com.eamois.domain.manual.Ensip>) 
				this.getHibernateTemplate().execute(new HibernateCallback() {

					@Override
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						// TODO Auto-generated method stub
						Criteria criteria = session
								.createCriteria(com.eamois.domain.manual.Ensip.class);
						criteria.add(Restrictions.eq("parentEnsip", ensip));
						return criteria.list();
					}
				});
	}

	@SuppressWarnings("unchecked")
	@Override
	public com.eamois.domain.manual.Ensip findEnsipByNameAndEngine(
			final String name, final Engine engine) {
		// TODO Auto-generated method stub
		return (com.eamois.domain.manual.Ensip) this.getHibernateTemplate()
				.execute(new HibernateCallback() {

					@Override
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						// TODO Auto-generated method stub
						Criteria criteria = session
								.createCriteria(com.eamois.domain.manual.Ensip.class);
						if(name!=null){
							criteria.add(Restrictions.eq("name", name));
						}
						if(engine !=null){
							criteria.add(Restrictions.eq("engine", engine));
						}
						ArrayList<com.eamois.domain.manual.Ensip> list = (ArrayList<com.eamois.domain.manual.Ensip>) criteria.list();
						if (list.size() == 0) {
							return null;
						} else {
							return list.get(0);
						}
					}
				});
	}

	@SuppressWarnings("unchecked")
	public Collection<Ensip> findEnsipByType(final EnsipType ensipType) {
		// TODO Auto-generated method stub
		return (Collection<Ensip>)this.getHibernateTemplate().execute(new HibernateCallback() {
			
			@Override
			public Object doInHibernate(Session session) throws HibernateException,
					SQLException {
				// TODO Auto-generated method stub
				Criteria criteria = session.createCriteria(Ensip.class);
				criteria.add(Restrictions.eq("ensipType", ensipType));
				return criteria.list();
			}
		});
	}

	@SuppressWarnings("unchecked")
	public Collection<Ensip> findEnsipByOther(final EnsipType ensipType) {
		// TODO Auto-generated method stub
		return (Collection<Ensip>)this.getHibernateTemplate().execute(new HibernateCallback() {
			
			@Override
			public Object doInHibernate(Session session) throws HibernateException,
					SQLException {
				// TODO Auto-generated method stub
				Criteria criteria = session.createCriteria(Ensip.class);
				criteria.add(Restrictions.eq("ensipType", ensipType));
				criteria.add(Restrictions.isNull("parentEnsip"));
				return criteria.list();
			}
		});
	}

	@SuppressWarnings("unchecked")
	@Override
	public Collection<Ensip> findEnsipByEngine(final Engine engine) {
		// TODO Auto-generated method stub
		return (Collection<Ensip>)this.getHibernateTemplate().execute(new HibernateCallback() {
			
			@Override
			public Object doInHibernate(Session session) throws HibernateException,
					SQLException {
				// TODO Auto-generated method stub
				Criteria criteria = session.createCriteria(Ensip.class);
				criteria.add(Restrictions.eq("engine", engine));
//				criteria.add(Restrictions.isNull("parentEnsip"));
				return criteria.list();
			}
		});
	}

}
